<template>
    <Form ref="formValidate" :model="formValidate" :rules="ruleValidate" :label-width="80">
        <FormItem label="姓名" prop="USER_NAME">
            <Input v-model="formValidate.USER_NAME" placeholder="请输入您的真实姓名"></Input>
        </FormItem>
        <FormItem label="手机号" prop="telphone">
            <Input v-model="formValidate.telphone" placeholder="请输入您的手机号"></Input>
        </FormItem>
        <FormItem label="学号" prop="USER_CODE">
            <Input v-model="formValidate.USER_CODE" @on-blur="method" error="errorMsg1" placeholder="请输入您的学号"></Input>
        </FormItem>
        <FormItem label="身份证号" prop="id_card">
            <Input v-model="formValidate.id_card" placeholder="请输入您的身份证号"></Input>
        </FormItem>
        <FormItem label="密码" prop="PASSWD">
            <Input type="password" v-model="formValidate.PASSWD" placeholder="请输入您的密码"></Input>
        </FormItem>
        <FormItem label="确认密码" prop="REPASSWD">
            <Input type="password" v-model="rePassword.REPASSWD" placeholder="请再次输入密码"></Input>
        </FormItem>

        <FormItem>
            <Button type="primary" @click="handleSubmit('formValidate')">注册</Button>
            <Button @click="handleReset('formValidate')" style="margin-left: 8px">重置</Button>
			<Button @click="goback()" style="margin-left: 8px">返回主页</Button>
        </FormItem>
    </Form>
</template>
<script>
import { save, selectOnlyOfCode } from '@/api/lky/LkyStudentRegister'
import { createModelObj } from '@/libs/util'
export default {
  data () {
    return {
      formValidate: {
        USER_NAME: '',
        telphone: '',
        USER_CODE: '',
        id_card: '',
        PASSWD: ''
      },
      rePassword: {
        REPASSWD: ''
      },
      // 接受后端传来的学号
      reUserCode: {
        RECode: ''
      },
      ruleValidate: {
        USER_NAME: [
          { required: true, message: '名字不能为空', trigger: 'blur' }
        ],
        telphone: [
          { required: true, message: '手机号不能为空', trigger: 'blur' },
          { pattern: /^1[34578]\d{9}$/, message: '手机格式有误！', trigger: 'blur' }
        ],
        USER_CODE: [
          { required: true, message: '学号不能为空', trigger: 'blur' }
        ],
        id_card: [
          { required: true, message: '身份证号不能为空', trigger: 'blur' },
          { pattern: /(^[1-9]\d{5}(18|19|([23]\d))\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$)|(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{2}$)/, message: '证件号码格式有误！', trigger: 'blur' }
        ],
        PASSWD: [
          { required: true, message: '密码不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    handleSubmit (name) {
      this.$refs[name].validate((valid) => {
        if (valid && this.formValidate.PASSWD == this.rePassword.REPASSWD) {
          save(Object.assign({}, createModelObj(this.formValidate, 'student'))).then(
						  res => {
						    if (res) {
						      this.$emit('addRow', res.row)
						      this.$emit('input', false)
						      this.handleReset(name)
						    }
						  }

          )

          // console.eval(res)
        } else if (this.formValidate.PASSWD != this.rePassword.REPASSWD) {
          this.$Message.error('两遍密码不一致!')
        }
      })
    },
	//检测学号唯一性
    method () {
      this.errorMsg1 = '',
      selectOnlyOfCode(Object.assign({}, createModelObj(this.formValidate, 'student'))).then(
        res => {
          if (res == 1) {
            this.$Message.error('学号已存在!')
          } 
        }
      )
    },

    handleReset (name) {
      this.$refs[name].resetFields()
    },
    goback () {
      window.location.href = '/home'
    }
  }
}
</script>
